<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="~@/assets/img/logo.svg" alt="logo" />
      <span class="title" v-if="!collapse">BaiYunXi</span>
    </div>
    <el-menu
      :default-active="defaultValue"
      class="el-menu-vertical"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
      :collapse="collapse"
    >
      <template v-for="item in userMenus" :key="item.id">
        <template v-if="item.type === 1">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <el-icon
                ><component :is="item.icon" v-if="item.icon"></component
              ></el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="sub in item.children" :key="sub.id">
              <el-menu-item :index="sub.id + ''" @click="handelItemClick(sub)">
                <el-icon v-if="sub.icon"
                  ><component :is="sub.icon"></component
                ></el-icon>
                <span>{{ sub.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <el-icon v-if="item.icon"
              ><component :is="item.icon"></component
            ></el-icon>
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { useStore } from '@/store'
import { useRouter, useRoute } from 'vue-router'
import { pathMapTomenu } from '@/utils/map-menus'
export default defineComponent({
  name: 'NavMenu',
  props: {
    collapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    const router = useRouter()
    const store = useStore()
    const userMenus = computed(() => {
      const data = store.state.login.userMenus
      const newdata = data.map((item: any) => {
        item.icon = item.icon
          .split('-')
          .slice(2)
          .map((item: string) => {
            return item.charAt(0).toUpperCase() + item.slice(1)
          })
          .join('')

        return item
      })
      return newdata
    })
    const route = useRoute()
    const currentPath = route.path
    //
    const menu = pathMapTomenu(userMenus.value, currentPath)
    const defaultValue = ref(menu.id + '')

    //

    const handelItemClick = (item: any) => {
      router.push({
        path: item.url ?? '/not-found'
      })
    }
    return {
      userMenus,
      handelItemClick,
      defaultValue
    }
  }
})
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;
  .logo {
    display: flex;
    position: sticky;
    top: 0;
    left: 0;
    z-index: 999;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    background-color: #001529;
    .img {
      height: 100%;
      margin: 0 10px;
    }
    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }
  .el-menu {
    border-right: none;
  }

  // 目录
  .el-sub-menu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
  }
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c3051 !important;
  }

  :deep(.el-sub-menu__title) {
    color: white;
    background-color: #03111f !important;
  }

  // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
  }

  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>
